﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

using WFL.Windows;

namespace WFL.Windows.Forms
{
    public static class ActivationHotKey
    {
        /// <summary>
        /// Extended key
        /// </summary>
        public const Keys Ext = (Keys)0x80000;

        /// <summary>
        /// 设置激活热键
        /// </summary>
        /// <param name="handle">窗口句柄</param>
        /// <param name="keys">热键</param>
        /// <returns>0：热键无效，1：窗口无效，2：成功，3：成功，但热键冲突</returns>
        public static int Set(IntPtr handle, Keys keys)
        {
            IntPtr wParam = (IntPtr)(((int)(keys & Keys.Modifiers) >> 8) | (int)(keys & Keys.KeyCode));
            int result = API.User32.SendMessage(handle, Header.Winuser.WM_SETHOTKEY, wParam, IntPtr.Zero).ToInt32();
            return result;
        }

        public static void Remove(IntPtr handle)
        {
            API.User32.SendMessage(handle, Header.Winuser.WM_SETHOTKEY, IntPtr.Zero, IntPtr.Zero).ToInt32();
        }

        public static Keys Get(IntPtr handle)
        {
            int hotKey = API.User32.SendMessage(handle, Header.Winuser.WM_GETHOTKEY, IntPtr.Zero, IntPtr.Zero).ToInt32();
            int modifiers = ((hotKey >> 8) & 0xff);
            int keyCode = (hotKey & 0xff);
            Keys keys = (Keys)((modifiers << 16) | keyCode);
            return keys;
        }
    }
}
